package com.webfinal.backsystem.service.impl;

import com.webfinal.backsystem.Util.ImageUtil;
import com.webfinal.backsystem.dao.ProductMapper;
import com.webfinal.backsystem.dao.ProductTypeMapper;
import com.webfinal.backsystem.entity.Product;
import com.webfinal.backsystem.entity.ProductType;
import com.webfinal.backsystem.request.product.AddProductRequest;
import com.webfinal.backsystem.request.product.QueryProductRequest;
import com.webfinal.backsystem.request.product.UpdateProductRequest;
import com.webfinal.backsystem.response.product.AddProductResponse;
import com.webfinal.backsystem.response.product.ProductInfoResponse;
import com.webfinal.backsystem.response.product.UpdateProductResponse;
import com.webfinal.backsystem.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductTypeMapper productTypeMapper;
    @Override
    public ProductInfoResponse queryProduct(QueryProductRequest request) {
        ProductInfoResponse response=new ProductInfoResponse();
        List<Product> productList=new ArrayList<>();
        if (request.getQueryFlag()==1){
            Product product=productMapper.selectByPrimaryKey(request.getProductId());
            if (product!=null){
                productList.add(product);
            }
        }
        if (request.getQueryFlag()==2){
            ProductType productType= productTypeMapper.selectByName(request.getProductType());
            productList=productMapper.selectByProductType(productType.getTypeCode());
        }
        if (request.getQueryFlag()==3){
            productList = productMapper.selectByProductName(request.getPruductName());
        }
        response.setProductList(productList);
        response.setResultCode("0");
        response.setResultMsg("");
        return response;
    }

    @Override
    public AddProductResponse addProduct(AddProductRequest addProductRequest) {
        AddProductResponse response = new AddProductResponse();
        Product product=new Product();
        product.setProductId(0);
        product.setProductName(addProductRequest.getProductName());
        product.setProductPrice(addProductRequest.getProductPrice());
        ProductType productType = productTypeMapper.selectByName(addProductRequest.getTypeName());
        if(productType!=null&&productType.getTypeCode()!=null){
            product.setTypeCode(productType.getTypeCode());
        }else {
            response.setResultCode("2");
            response.setResultMsg("error");
            return response;
        }
        product.setStock(addProductRequest.getStock());
        product.setIntroduction(addProductRequest.getIntroduction());
        product.setSpecifications(addProductRequest.getSpecifications());
        product.setImage(addProductRequest.getImage());
        product.setOnShow(addProductRequest.getOnShow());
        if(productMapper.insert(product)==1){
            response.setResultCode("0");
            response.setResultMsg("success");
        }else {
            response.setResultCode("1");
            response.setResultMsg("error");
        }

        return response;
    }

    @Override
    public UpdateProductResponse updateProduct(UpdateProductRequest updateProductRequest) {
        UpdateProductResponse response = new UpdateProductResponse();
            Product product = productMapper.selectByProName(updateProductRequest.getProductName());
            if(product!=null){

//                product.setProductPrice(updateProductRequest.getProductPrice());
//                ProductType productType = productTypeMapper.selectByName(updateProductRequest.getTypeName());
//                if(productType!=null&&productType.getTypeCode()!=null){
//                    product.setTypeCode(productType.getTypeCode());
//                }
//                product.setStock(updateProductRequest.getStock());
//                product.setIntroduction(updateProductRequest.getIntroduction());
//                product.setSpecifications(updateProductRequest.getSpecifications());
//                product.setImage(updateProductRequest.getImage());
                product.setOnShow(updateProductRequest.getOnShow());
                if(productMapper.updateByPrimaryKey(product)==1){
                    response.setResultCode("0");
                    response.setResultMsg("success");
                }else {
                    response.setResultCode("1");
                    response.setResultMsg("error");
                }
            }
        return response;
    }

    @Override
    public AddProductResponse coverUpload(MultipartFile image) {
        AddProductResponse response =new AddProductResponse();
        String temp = ImageUtil.save(image);
        if (temp!=""&&temp!=null){
            response.setResultCode("0");
            response.setResultMsg("success");
            response.setProImage(temp);
        }
        else {
            response.setResultCode("1");
            response.setResultMsg("error");
        }
        return response;
    }
}
